package ws;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import metier.IVoyageAvionMetier;
import metier.IVoyageTrainMetier;
import model.VoyageAvion;
import model.VoyageCar;
import model.VoyageTrain;
import dao.IVoyageCarDao;

@Stateless
@Path("/travelservice")
public class TravelWebService {

	@EJB
	private IVoyageCarDao voyagecar;

	@EJB
	private IVoyageAvionMetier voyageavion;

	@EJB
	private IVoyageTrainMetier voyagetrain;

	@GET
	@Path("/find/{villedepart}/{villearrivee}")
	@Produces(MediaType.APPLICATION_JSON)
	public JsonResponse findmyway(@PathParam("villedepart") String villeDepart,
			@PathParam("villearrivee") String villeArrivee) {
		Allways resultat;
		String arrivetrain = "CASA VOYAGEURS";
		if (!villeDepart.equals(" ")) {
			if (!villeArrivee.equals(" ")) {
				resultat = new Allways(
						voyagecar.find(villeDepart, villeArrivee), voyageavion
								.find(villeDepart, villeArrivee)
								.getHorairesavion(), voyagetrain.find(
								villeDepart, arrivetrain).getHorairesTrain());
				return new JsonResponse(new Header("OK", ""), resultat);

			} else {
				return new JsonResponse(new Header("NOK",
						"ville destination vide"), null);
			}
		} else {
			return new JsonResponse(new Header("NOK", "ville depart vide"),
					null);
		}
	}

	@GET
	@Path("/findtrain/{villedepart}/{villearrivee}")
	@Produces(MediaType.APPLICATION_JSON)
	public JsonResponse findTrain(@PathParam("villedepart") String villeDepart,
			@PathParam("villearrivee") String villeArrivee) {
		VoyageTrain resultat;
		if (!villeDepart.equals(" ")) {
			if (!villeArrivee.equals(" ")) {
				resultat = voyagetrain.find(villeDepart, villeArrivee);
				if (resultat.getHorairesTrain().size() != 0)
					return new JsonResponse(new Header("OK", ""), resultat);
				else
					return new JsonResponse(new Header("NOK",
							"Aucun voyage n'est disponible pour ce trajet"),
							null);

			} else {
				return new JsonResponse(new Header("NOK",
						"ville destination vide"), null);
			}
		} else {
			return new JsonResponse(new Header("NOK", "ville depart vide"),
					null);
		}
	}

	@GET
	@Path("/findfly/{villedepart}/{villearrivee}")
	@Produces(MediaType.APPLICATION_JSON)
	public JsonResponse findFly(@PathParam("villedepart") String villeDepart,
			@PathParam("villearrivee") String villeArrivee) {
		VoyageAvion resultat;
		if (!villeDepart.equals(" ")) {
			if (!villeArrivee.equals(" ")) {
				resultat = voyageavion.find(villeDepart, villeArrivee);
				if (resultat.getHorairesavion().size() != 0)
					return new JsonResponse(new Header("OK", ""), resultat);
				else
					return new JsonResponse(new Header("NOK",
							"Aucun vol n'est disponible pour ce trajet"), null);

			} else {
				return new JsonResponse(new Header("NOK",
						"ville destination vide"), null);
			}
		} else {
			return new JsonResponse(new Header("NOK", "ville depart vide"),
					null);
		}
	}

	@GET
	@Path("/findcar/{villedepart}/{villearrivee}")
	@Produces(MediaType.APPLICATION_JSON)
	public JsonResponse findCar(@PathParam("villedepart") String villeDepart,
			@PathParam("villearrivee") String villeArrivee) {
		VoyageCar resultat;
		if (!villeDepart.equals(" ")) {
			if (!villeArrivee.equals(" ")) {
				resultat = voyagecar.find(villeDepart, villeArrivee);
				if (resultat != null)
					return new JsonResponse(new Header("OK", ""), resultat);
				else
					return new JsonResponse(new Header("NOK",
							"Aucun voyage n'est disponible pour ce trajet"),
							null);

			} else {
				return new JsonResponse(new Header("NOK",
						"ville destination vide"), null);
			}
		} else {
			return new JsonResponse(new Header("NOK", "ville depart vide"),
					null);
		}
	}
}
